﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using _4T.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Transactions;
using System.Globalization;
using Notification;

namespace _4T.DAL
{
    public class NotificationDAL
    {
        public List<Data.Notification> getListDevice(int role)
        {
            ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;

                SqlConnection conn = new SqlConnection(conString);
                SqlCommand cmd = new SqlCommand();
                SqlDataReader reader = null;
                try
                {
                    conn.Open();
                    cmd.CommandText = "NotificationGetByRole";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = conn;
                    cmd.Parameters.Add("@role", SqlDbType.Int).Value = role;
                    reader = cmd.ExecuteReader();
                    if (reader != null)
                    {
                        List<Data.Notification> list = new List<Data.Notification>();
                        while (reader.Read())
                        {
                            Data.Notification item = GetNotificationFromReader(reader);
                            list.Add(item);
                        }
                        return list;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    cmd.Dispose();
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }

            return null;
        }

        private Data.Notification GetNotificationFromReader(SqlDataReader reader)
        {
            try
            {
                string DeviceID = string.Empty;
                DEVICE_TYPE DeviceType = 0;
                string PersonalCode = string.Empty;
                int PersonalRole = 0;
                DateTime loginDatetime = DateTime.Now;

                if (reader.GetValue(0) != DBNull.Value)
                {
                    DeviceID = reader.GetString(0);
                }

                if (reader.GetValue(1) != DBNull.Value)
                {
                    int deviceTypeInt = reader.GetInt32(1);
                    if (deviceTypeInt == 0)
                        DeviceType = DEVICE_TYPE.Apple;
                    else
                    {
                        if (deviceTypeInt == 1)
                            DeviceType = DEVICE_TYPE.Android;
                    }
                }

                if (reader.GetValue(2) != DBNull.Value)
                {
                    PersonalCode = reader.GetString(2);
                }

                if (reader.GetValue(3) != DBNull.Value)
                {
                    loginDatetime = reader.GetDateTime(3);
                }

                if (reader.GetValue(4) != DBNull.Value)
                {
                    PersonalRole = reader.GetInt32(4);
                }

                Data.Notification notification = new Data.Notification();
                notification.DeviceID = DeviceID;
                notification.DeviceType = DeviceType;
                notification.PersonalCode = PersonalCode;
                notification.PersonalRole = PersonalRole;
                notification.LoginDatetime = loginDatetime;

                return notification;
            }
            catch
            {
                return null;
            }
        }

        public void sendNotification(string message, int role)
        {
            try
            {
                List<Data.Notification> listDevice = this.getListDevice(role);

                if (listDevice != null && listDevice.Count != 0)
                {
                    for (int i = 0; i < listDevice.Count; i++)
                    {
                        Notification.Notification notification = new Notification.Notification(listDevice[i].DeviceType);
                        notification.sendNotification(listDevice[i].DeviceID, message);
                    }
                }
            }
            catch (Exception ex)
            {
                Global.logger = log4net.LogManager.GetLogger(typeof(NotificationDAL));
                Global.logger.Error(ex.ToString());
            }
        }

        public int InsertDeviceID(string device_id, int device_type, string personal_code)
        {
            int result = 0;
            ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;

                SqlConnection conn = new SqlConnection(conString);
                SqlCommand cmd = new SqlCommand();
                try
                {
                    conn.Open();
                    cmd.CommandText = "NotificationInsert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@device_id", SqlDbType.NVarChar, 255).Value = device_id;
                    cmd.Parameters.Add("@device_type", SqlDbType.Int, 1).Value = device_type;
                    cmd.Parameters.Add("@personal_code", SqlDbType.NVarChar, 50).Value = personal_code;
                    cmd.Connection = conn;

                    result = Convert.ToInt32(cmd.ExecuteScalar());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    cmd.Dispose();
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }

            return result;
        }

    }
}